package com.ebelter.bodyfatscale.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.util.ScaleBaseUtils;


@SuppressLint("AppCompatCustomView")
public class MyPswEd extends RelativeLayout{

    public MyPswEd(Context context) {
        this(context,null);
    }

    public MyPswEd(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    String title1="";
    String hint1="";

    EditText textView;
    EditText editText;

    public MyPswEd(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyPswEd);
        if (typedArray != null) {
            title1 = typedArray.getString(R.styleable.MyPswEd_MyPswEd_title1);
            hint1 = typedArray.getString(R.styleable.MyPswEd_MyPswEd_hint1);
            typedArray.recycle();
        }

        initView(getContext());



    }

    private void initView(Context context) {
        View view =  View.inflate(context, R.layout.mypswed_ly, this);//把那个布局当做是自己的孩子

        textView = view.findViewById(R.id.mypsw_tv2);

        editText = view.findViewById(R.id.mypsw_ed2);
        //editText.setAlpha(0f);



        ScaleBaseUtils.updateGravityByLocal(editText,textView);


        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,editText.getTextSize());
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                title1=s.toString();
                updateTitle1();

            }
            @Override
            public void afterTextChanged(Editable s) {}
        });

        updateTitle1();

    }

    private void updateTitle1(){
        if (TextUtils.isEmpty(title1)){
            textView.setText(hint1);
        }else {
            StringBuilder stringBuilder=new StringBuilder();
            for (int i=0;i<title1.length();i++){
                stringBuilder.append("•");//•·●●♦•
            }
            textView.setText(stringBuilder.toString());
        }
    }

    public String getText() {
        return title1;
    }
}
